/* 
Creates the figures and tables for the analysis of the CUT reform in the establishment sample
	// input: contract_wageadjustment.txt, contracts_cnes, contracts_act, estlevel_dataset, microregions_municipality_concordance
	// output: 
	- contract_wageadjustment.dta; 
	- Table7.xlsx (sheets: wageadj_table)	
*/ 

cap log close
cap log using "$logs/wage_adjustments_log", replace


********************************************************

* Load data on wage adjustments
import delimited "$raw\contract_wageadjustment.txt", delimiter("|") varnames(1) encoding(utf8) clear

* Put each adjustment % as an inidividual variable
split wage_adjustments, parse(/)
drop wage_adjustments

* Clean and destring each wage adjustment
foreach xxx of varlist wage_adjustment* {
	//remove years
	replace `xxx' = subinstr(`xxx',"2007","",.)
	replace `xxx' = subinstr(`xxx',"2008","",.)
	replace `xxx' = subinstr(`xxx',"2009","",.)
	replace `xxx' = subinstr(`xxx',"2010","",.)
	replace `xxx' = subinstr(`xxx',"2011","",.)
	replace `xxx' = subinstr(`xxx',"2012","",.)
	replace `xxx' = subinstr(`xxx',"2013","",.)
	replace `xxx' = subinstr(`xxx',"2014","",.)
	replace `xxx' = subinstr(`xxx',"2015","",.)
	replace `xxx' = subinstr(`xxx',"2016","",.)
	replace `xxx' = subinstr(`xxx',"2017","",.)
	replace `xxx' = subinstr(`xxx',"2018","",.)
	//remove double periods
	forvalues i = 1/10 {
	replace `xxx' = subinstr(`xxx',"..",".",.)
	}
	//destring
	destring `xxx', replace
	//ignore 0% adjustments and adjustments >=20%
	replace `xxx' = . if `xxx'==0
	replace `xxx' = . if `xxx'>=20
}

* Keep the maximum adjustment per CBA
egen wage_adj = rowmax(wage_adjustments*)
sum wage_adj, d

* Save
sort contract_id
keep contract_id wage_adj
save "$files\contract_wageadjustment.dta", replace

********************************************************

* Get the CBAs from our sample with a wage adjustment
use "$files/unions/contracts_cnes.dta", clear
keep contract_id employer_id union_id 
//from sample
merge m:1 contract_id using "$files/contracts_act.dta"
keep if _merge == 3
drop _merge 
//with wage adjustment
merge m:1 contract_id using "$files/contract_wageadjustment.dta"
keep if _merge == 3
drop _merge 
mdesc wage_adj
keep if wage_adj!=.
//per employer year, keep CBA with largest wage adjustment
keep contract_id employer_id year wage_adj
gen xxx1 = -wage_adj
gen xxx2 = runiform()
sort employer_id year xxx1 xxx2, stable
egen yyy = tag(employer_id year)
keep if yyy==1
drop xxx* yyy
duplicates drop
//save to merge
gen fakeid_estab = subinstr(employer_id,".","",.)
replace fakeid_estab = subinstr(fakeid_estab,"/","",.)
replace fakeid_estab = subinstr(fakeid_estab,"-","",.)
destring fakeid_estab, replace
tempfile temp
save `temp'


**********************************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (PANEL A: CBA wage adj) **
**********************************************************************************

* Analysis sample (establishment level)
use "$files/estlevel_dataset.dta", clear
keep if bl_bothFM==1
keep if (bl_covered==1)
keep if (bl_signing==1)

* time varying fixed effects
//industry
gen ind2d_num = floor(ind_mode/1e3)
egen indyrFE = group(year ind2d_num)
//geography
gen state_mode = floor(municipality_mode/1e4)
rename municipality_mode municipality
merge m:1 municipality using "$raw/microregions_municipality_concordance.dta", keepusing(microregion)
drop if _merge==2
drop _merge
rename municipality municipality_mode
rename microregion microregion_mode
egen geogyrFE = group(year microregion)

* for clustering
egen clustergrp = group(fakeid_estab)

* event study var 
gen post=(year>=2015) if year!=.
gen treatpost=treat*post
	tab year, gen(yearj)
	
forvalues i = 1/7 {
	gen interj`i'=treat*yearj`i'
}

lab var interj1 "2011"
lab var interj2 "2012"
lab var interj3 "2013"
lab var interj4 "2014"
lab var interj5 "2015"
lab var interj6 "2016"
lab var interj7 "2017"

gen zero=0
lab var zero "2014"
replace interj4 = zero

drop if year==2011
drop interj1
merge 1:1 fakeid_estab year using `temp'


* Results

preserve

	drop if _merge==2
	xtset fakeid_estab year

	replace wage_adj = 0 if wage_adj==.
	label var wage_adj "Wage adjustment (%)"

	collapse (mean) wage_adj ///
			 (firstnm) treat ind2d_num state_mode microregion_mode clustergrp ///
			 , by(fakeid_estab post)
	tab treat
	gen treatpost = treat * post
	egen indyrFE = group(post ind2d_num)
	egen geogyrFE = group(post microregion_mode)

	sum wage_adj if post==0 & treat==1
	reghdfe wage_adj treatpost, absorb(fakeid_estab *FE) cluster(fakeid_estab)
		sum wage_adj if treat == 1 & post==0
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N)
		estimates store a1
	
restore

#d ;
estout a1 using "$tables/wageadj_table.txt", 
style(tab) mlabels(none) label collabels(none)
cells(b(star fmt(%9.3f)) se(par)) 
stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
#d cr

import delimited "$tables/wageadj_table.txt", clear
rename v1 coeffs
rename v2 wage_adj_prepost
export excel using "$tables/Table7.xlsx", sheet("wageadj_table") cell(A1) firstrow(var) sheetreplace


cap log close
